<script setup lang="ts">
import { ref } from "vue";
import useConsultStore from "@/pinia/modules/consultStore.ts";
import type { Image } from "@/types/consultTypes";
import { UploaderAfterRead } from "vant/lib/uploader/types";
import { UploaderFileListItem } from "vant";

const emits = defineEmits<{
    (eventName: "sendText", text: string): void
    (eventName: "sendImg", img: Image): void
}>();

const props = defineProps<{
    disabled: boolean
}>();


const consultStore = useConsultStore();

const text = ref<string>("");


/*输入框输入文字后按下回车的回调函数*/
const enter = () => {
    emits("sendText", text.value);
    text.value = "";
};

/*上传图片的回调函数*/
const afterRead: UploaderAfterRead = async (fileParams: UploaderFileListItem | UploaderFileListItem[]) => {
    if (Array.isArray(fileParams)) return;
    if (!fileParams.file) return;
    const formData: FormData = new FormData();
    formData.append("file", fileParams.file);
    const result: Image = await consultStore.upload(formData);
    emits("sendImg", result);
};

</script>

<template>
    <div class="room-action">
        <van-field type="text" class="input" :border="false" placeholder="问医生" autocomplete="off" :disabled="disabled" v-model="text" @keyup.enter="enter"></van-field>
        <van-uploader :preview-image="false" :disabled="disabled" :after-read="afterRead">
            <svg-icon svg-name="img" />
        </van-uploader>
    </div>
</template>

<style lang="scss" scoped>
.room-action {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 60px;
    background-color: #fff;
    display: flex;
    align-items: center;
    padding: 0 16px;
    z-index: 1;
    box-sizing: border-box;

    .input {
        background-color: var(--cp-bg);
        border: none;
        border-radius: 25px;
        margin-right: 10px;
        padding: 8px 15px;
    }
}
</style>
